﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using FBA.Client.Utils;

namespace FBA.Client.Data
{
    /// <summary>
    /// 真正从后台获取通用数据的接口都在此类中
    /// </summary>
    public partial class DataCache
    {
        //第一个参数表示日志ID（用来区分多个日志信息之间的关系），第二个参数是日志内容
        public static Action<LoadingEvent> LogHandler { get; set; }

        public static DataTable ExpressModel { get; private set; }
        public static DataTable Role { get; private set; }
        public static DataTable Organization { get; private set; }
        public static DataTable ServiceProvider { get; private set; }
        public static DataTable ServiceType { get; private set; }
        public static DataTable SortingRule { get; private set; }
        public static DataTable Price { get; private set; }
        public static DataTable TrackingNetwork { get; private set; }
        public static DataTable Currency { get; private set; }
        public static DataTable Schedule { get; private set; }
        public static DataTable User { get; private set; }
        public static DataTable Client { get; private set; }
        public static DataTable Destination { get; private set; }


        //多线程初始化数据
        public static void Load()
        {
            ThreadPool.SetMinThreads(1, 1);
            ThreadPool.SetMaxThreads(3, 3);

            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadExpressModel()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadRole()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadOrganization()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadServiceProvider()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadServiceType()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadSortingRule()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadPrice()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadTrackingNetwork()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadCurrency()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadSchedule()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadUser()));
            ThreadPool.QueueUserWorkItem(new WaitCallback(state => LoadClient()));

            Console.WriteLine("主线程结束！");
        }

        private static void Info(LoadingEvent e)
        {
            if (LogHandler != null && e != null)
            {
                LogHandler.Invoke(e);
            }
        }

        /// <summary>
        /// 线路渠道
        /// </summary>
        public static DataTable LoadExpressModel()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.ExpressModel");

            string sql = @"
                    select t.id,
                            t.code,
                            t.name,
                            t.service_type_id,
                            t.tracking_network,
                            t.sorting_id,
                            t.price_list_id,
                            t.multiple_pieces,
                            t.volumetric_method,
                            t.volumetric_ratio,
                            t.support_site,
                            t.published,
                            t.print_pick,
                            t.position,
                            t.status,
                            t.memo,
                            t.create_user,
                            t.create_date,
                            t.modify_user,
                            t.modify_date
                        from t_express_model t
                        where t.status = 1
                    ";

            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                ExpressModel = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return ExpressModel;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 角色
        /// </summary>
        public static DataTable LoadRole(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.Role");

            string sql = @"
            select t.id,
                    t.name,
                    t.description,
                    t.permission,
                    t.create_user,
                    t.create_date,
                    t.modify_user,
                    t.modify_date,
                    t.status
                from t_role t where status = 1
            ";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                Role = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return Role;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 组织机构
        /// </summary>
        public static DataTable LoadOrganization(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.Organization");

            string sql = @"
                select t.id,
                        t.name,
                        t.alias,
                        t.type,
                        t.address,
                        t.contact,
                        t.phone,
                        t.create_user,
                        t.create_date,
                        t.modify_user,
                        t.modify_date,
                        t.status,
                        t.memo
                    from t_organization t
                    where t.status = 1
            ";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                Organization = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return Organization;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 服务商
        /// </summary>
        public static DataTable LoadServiceProvider(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.ServiceProvider");

            string sql = @"
                    select t.id,
                            t.name,
                            t.address,
                            t.contact,
                            t.phone,
                            t.account,
                            t.status,
                            t.memo
                    from t_service_provider t
            ";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                ServiceProvider = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return ServiceProvider;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 服务方式
        /// </summary>
        public static DataTable LoadServiceType(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.ServiceType");

            string sql = @"
                select t.id,
                        t.name,
                        t.create_user,
                        t.create_date,
                        t.modify_user,
                        t.modify_date,
                        t.status,
                        t.memo,
                        t.api_code,
                        t.service_provider_id
                    from t_service_type t
                    where status = 1
            ";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                ServiceType = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return ServiceType;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 智能分拣
        /// </summary>
        public static DataTable LoadSortingRule(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.SortingRule");

            string sql = @"
                select t.id,
                        t.name,
                        t.description,
                        t.rule,
                        t.status,
                        t.create_user,
                        t.create_date,
                        t.modify_user,
                        t.modify_date
                    from t_sorting_rule t
                    where t.status = 1
            ";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                SortingRule = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return SortingRule;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 价格表
        /// </summary>
        public static DataTable LoadPrice(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.PriceList");

            string sql = @"
            select t.id,
                    t.name,
                    t.customs_fees,
                    t.fuel_costs,
                    t.discount,
                    t.create_user,
                    t.create_date,
                    t.modify_user,
                    t.modify_date,
                    t.status,
                    t.memo
                from t_price t
                where t.status = 1
        ";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                Price = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return Price;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 追踪网络(查询前100条)
        /// </summary>
        public static DataTable LoadTrackingNetwork(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.TrackingNetwork");

            string sql = @"
                select t.id,
                        t.name,
                        t.url,
                        t.create_user,
                        t.create_date,
                        t.modify_user,
                        t.modify_date,
                        t.status,
                        t.memo
                    from t_tracking_network t where rownum<=100
            ";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                TrackingNetwork = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return TrackingNetwork;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 币种
        /// </summary>
        public static DataTable LoadCurrency(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.Currency");

            string sql = @"SELECT T.NAME, T.EXCHANGE_RATE FROM T_CURRENCY T";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                Currency = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return Currency;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 执行计划
        /// </summary>
        public static DataTable LoadSchedule(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.Schedule");

            string sql = @"select t.key,t.value from t_config t where t.key like 'JOB.%'";
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                Schedule = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return Schedule;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 内部用户
        /// </summary>
        public static DataTable LoadUser(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.User");

            string sql = @"select
                            u.id,
                            u.account,
                            u.name,
                            u.create_date
                            from t_user u
                            order by u.id desc
                            ";
          
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                User = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return User;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }

        /// <summary>
        /// 客户信息
        /// </summary>
        public static DataTable LoadClient(object state = null)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.Client");

            string sql = @"select
                            u.id,
                            u.account,
                            u.name,
                            u.manager_name as manager_name,
                            u.create_date
                            from t_client u
                            order by u.id desc
                        ";
         
            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                Client = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return Client;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }


        /// <summary>
        /// 目的地
        /// </summary>
        public static DataTable LoadDestination()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            string id = Language.Get("Lang.Des");

            string sql = @"select t.id,t.name,t.postcode_expression,t.phone_expression,t.code,t.alias from t_des t where t.status = 1";

            Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Start));
            try
            {
                Destination = DataRequest.Query(sql, DataColumnUtils.IsSelectedDefaultFalse);
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Success));
                return Destination;
            }
            catch (Exception e)
            {
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Failed, e.Message));
                return null;
            }
            finally
            {
                watch.Stop();
                Info(new LoadingEvent(id, LoadingEvent.LoadingEventType.Finish, watch.ElapsedMilliseconds));
            }
        }


        
    }
}